import {defineStore} from "pinia";
import {ElMessage} from "element-plus";
import {addNotices, delNotices, findAllNotices, getNotices, updateNotices, updateNoticesRecord} from "@/api/notices";
export const NoticesStore = defineStore('notices', {
    state:()=>{
        return {
            // 列表
            noticesList: [],
            // 详细信息
            noticesInfo: {}
        }
    },
    actions:{
        updateNoticesRecord(id,type){
            return new Promise((resolve, reject)=>{
                updateNoticesRecord(id,{type}).then(res => {
                    resolve(res.code)
                }).catch(err => {
                    reject()
                })
            })
        },
        async getNoticesList(query) {
            return new Promise((resolve, reject)=>{
                findAllNotices(query).then(res => {
                    this.noticesList = res.result.list
                    resolve(res.result)
                }).catch(err => {
                    reject()
                })
            })
        },
        async getNoticesInfo(id) {
            const {result} = await getNotices(id)
            this.noticesInfo = result
            this.noticesInfo.notices_price = parseFloat(this.noticesInfo.notices_price)
        },
        updateNotices(id,data) {
            return new Promise(async (resolve, reject) => {
                updateNotices(id,data).then(res => {
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        },
        async deleteNotices(id) {
            const {code} = await delNotices(id)
        },
    }
})